<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/rights">角色列表</a></el-breadcrumb-item>
      <el-breadcrumb-item>角色权限管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 20px">
      <el-button @click="addDialogVisible = true">添加角色</el-button>
      <el-table
          :data="roleList"
          style="width: 100%"
          stripe>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <!-- 一级权限 -->
              <el-row class="vcenter" v-for="(item1) in scope.row.children" :key="item1.id">
                <el-col :span='5'>
                  <el-tag closable @close ="removeRight(scope.row, item1.id)">{{item1.authName}}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span='19'>
                  <!-- 二级权限 -->
                  <el-row class="vcenter" v-for="item2 in item1.children" :key="item2.id">
                    <el-col :span='6'>
                      <el-tag type="success" closable @close ="removeRight(scope.row, item2.id)">{{item2.authName}}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <!-- 三级权限 -->
                    <el-col class="vcenter" :span="18" >
                      <el-tag closable
                      v-for="item3 in item2.children" :key="item3.id"
                      @close ="removeRight(scope.row, item3.id)"
                      type="warning">
                      {{item3.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index"></el-table-column>
          <el-table-column prop="roleName" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="roleDesc" label="角色描述" width="180">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete" @click="deleteRole(scope.row)">删除</el-button>
              <el-button type="warning" icon="el-icon-setting" @click="setRightDialog(scope.row)">权限分配</el-button>
            </template>
          </el-table-column>
      </el-table>
    </el-card>
    <!-- 删除角色对话框 -->
    <el-dialog
    title="删除角色"
    :visible.sync= "deleteRoleDialogVisible"
    width="30%">
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
    <!-- 分配权限的对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="dialogVisible" @close= "dialogClose()"
      width="50%">
      <el-tree
        :data= "rightList" :props= "treeProps" :default-checked-keys= "rightHave" node-key="id"
        ref="treeRef"
        show-checkbox
        @></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="rightSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色列表
      roleList: [],
      dialogVisible: false,
      deleteRoleDialogVisible: false,
      // 权限列表
      rightList: [],
      // 角色已有权限
      rightHave: [],
      // 权限树
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 当前选中的角色
      roleId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色
    async getRoleList() {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        return this.$message.error('获取角色列表失败')
      }
      this.roleList = res.data
    },
    // 删除角色
    deleteRole() {
    },
    // 移除权限
    removeRight(role, rightId) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
        if (res.meta.status !== 200) {
          return this.$message.error('操作失败')
        }
        role.children = res.data
      }).catch(() => {
      })
    },
    // 权限分配
    async setRightDialog(role) {
      this.roleId = role.id
      this.dialogVisible = true
      const { data: res } = await this.$http.get('rights/tree')
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限列表失败')
      }
      this.rightList = res.data
      this.getRoleRights(role, this.rightHave)
    },
    // 通过递归方式获取角色下已有三级权限
    getRoleRights(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getRoleRights(item, arr)
      })
    },
    dialogClose() {
      this.rightHave = []
    },
    // 提交分配好的权限
    async rightSubmit() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      console.log(keys)
      const idstr = keys.join(',')
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: idstr })
      if (res.meta.status !== 200) {
        return this.$message.error('分配权限失败')
      }
      this.$message.success('分配权限成功')
      this.getRoleList()
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag{
  margin: 10px;
}
.el-row{
  border-bottom: 1px solid #eee;
}
.el-row:first-child{
  border-top: 1px solid #eee;
}
.vcenter{
  display: flex;
  align-items: center;
}
</style>
